//
//  SketchUpdateController.h
//  RunSketch
//
//  Created by Brian Hinch on 7/27/08.
//  Copyright 2008 Tellart. All rights reserved.
//

#import <UIKit/UIKit.h>
#define kURLTimeoutInterval         60.0
#define kDownloadTimerInterval      0.05

#define kSketchUpdateURLFormatString    @"http://%@:8080/sketches/index.php"

#define kCAT_EL_CATALOG             @"catalog"
#define kCAT_EL_ASSET               @"asset"
#define kCAT_ATR_TYPE               @"type"
#define kCAT_ATR_BASE               @"base"
#define kCAT_ATR_SRC                @"src"
#define kCAT_ATR_SIZE               @"size"
#define kCAT_ATR_FILES              @"files"
#define kASSET_TYPE_DIR             @"dir"
#define kASSET_TYPE_FILE            @"file"

#define kSketchIndexFileName            @"index"
#define kSketchIndexFileType            @"html"
#define kLiveSketchDirectoryName        @"Sketch"
#define kUpdatingSketchDirectoryName    @"Incoming"

@class RootViewController;

@interface SketchUpdateController : NSObject  {

    RootViewController  *watcher;

    NSMutableData       *receivedData;
    NSURLConnection     *theConnection;
    NSURL               *rootURL;
    NSString            *baseURL;
    NSString            *sLiveSketchBasePath;
    NSString            *sUpdatingSketchBasePath;

    BOOL                updating;

    long                currentDownloadIndex;
    long                totalNumDownloads;

    BOOL                downloading;
    NSMutableArray      *downloadRelativePathsQue;

    NSString            *lastElementName;
    int                 sketchBytesTotal;
    int                 sketchBytesLoaded;
}


- (void)setWatcher:(RootViewController *)w;
- (void)updateFromURL:(NSURL *)url;
- (void)stop;

-(void) event_didFailWithConnectionError;
-(void) event_didFailWithCommunicationError:(NSString *)msg;
- (void) event_didFailWithCatalogParserError:(NSError *)err;
- (void) event_didLoadCatalog;
-(void) event_madeProgress: (int)bytesLoaded: (int)bytesTotal;
-(void) event_didSucceed;
-(void) event_didStop;

- (void) startDownloads;
- (BOOL)prepareFilesystem;
-(BOOL)replaceLiveSketchWithUpdatingSketch;

- (void)connectionDidFinishLoading:(NSURLConnection *)connection;
- (void)didFinishLoadingFile:(NSURLConnection *)connection;
- (void)didFinishLoadingCatalog:(NSURLConnection *)connection;
+ (id) getSketchEntryPointURL;

@end
